<?php
require_once(dirname(__FILE__).'/../../../uc/models/user/UCModUserInfo.php');


/**
 * UCModUserInfo类用于查询/更新用户资料
 */
class OpModUserInfo extends UCModUserInfo{

    protected static $attributesMap = array(
       'name' => USERINFO_NICKNAME,
       'tinyurl' => USERINFO_AVATAR_30,
       'headurl' => USERINFO_AVATAR_90,
       'sex'    => USERINFO_GENDER_DESC,
       'birthday' => USERINFO_BIRTH_DAY,
       'tinyurl'  => USERINFO_AVATAR_30,
       'headurl'  => USERINFO_AVATAR_90,
       'mainurl'  => USERINFO_AVATAR_175,
       'home_location' => USERINFO_HOMETOWN_DESC
    );

    public static function getUserInfoByAttributes($user, $attributes)
    {
        $field = array();
        foreach ($attributes as $attr)
        {
            if (isset(self::$attributesMap[$attr]))
            {
                $field[] = self::$attributesMap[$attr];
            }
        }
        
        
        $data = self::getUserInfo($user, $field);
        if (false === $data)
        {
            return false;
        }
        
        $result = array('id' => $user);
        $flip_attr = array_flip(self::$attributesMap);
        foreach($data as $key => $value)
        {
            $result[$flip_attr[$key]] = $value;
        }
        
        if (false !== array_search("work_info", $attributes))
        {
            $educationResult = array();
            $model = new OpModUserInfo($user);
            $data = $model->getCompanyList();
            if (false === $data)
            {
                return false;
            }
            
            foreach ($data as $entry)
            {
                $temp = array();
                $temp['name'] = $entry['company'];
                $temp['position'] = $entry['position'];
                $temp['startYear'] = $entry['startYear'];
                $temp['endYear'] = $entry['endYear'];
                
                $educationResult[] = $temp;
            } 
            
            $result["work_info"] = $educationResult;
        }
        
        if (false !== array_search("education_info", $attributes))
        {
            $educationResult = array();
            $model = new OpModUserInfo($user);
            $data = $model->getSchoolList();
            
            if (false === $data)
            {
                return false;
            }
            
            foreach ($data as $entry)
            {
                $temp = array();
                $temp['name'] = $entry['schoolName'];
                $temp['year'] = $entry['startYear'];
                $temp['department'] = $entry['major'];
                
                $educationResult[] = $temp;
            } 
            
            $result["education_info"] = $educationResult;
        }
        
        
        return $result;
    }
}